//
//  MXPieCharView.m
//  MXArcChart
//
//  Created by 马彦 on 16/11/8.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXPieChartView.h"
#import "MXCircleArcView.h"

@interface MXPieChartView ()<BottomViewDelegate>

@property (nonatomic, strong) UILabel *titleNameLbl;
@property (nonatomic, assign) CGSize titleSize;

@property (nonatomic, strong) MXCircleArcView *circleArcView;
@property (nonatomic, strong) MXBottomView *bottomView;
@property (nonatomic, assign) CGFloat bottomViewWidth;

/**  按钮 */
@property (nonatomic, strong) UIButton *bottomBtn;

@end

@implementation MXPieChartView


- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.circleArcView];
        [self addSubview:self.titleNameLbl];
        [self addSubview:self.bottomView];
        self.bottomViewStyle = PositionStyleCenter;
        [self addSubview:self.bottomBtn];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleSize = [self.titleName boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
    self.circleArcView.frame = CGRectMake(self.setOffLeft, self.setOffTop, self.width - self.setOffLeft - self.setOffRight, self.height - self.setOffTop - self.setOffBottom);
    self.titleNameLbl.frame = CGRectMake(10, 5, self.width - 20, self.titleSize.height + 4);
    
    self.bottomView.nameArray = self.nameArray;
}


/**
 *  圆弧与边缘的间距
 *
 *  @param left   左间距
 *  @param top    顶部间距
 *  @param right  右间距
 *  @param bottom 底部间距
 */
- (void)setExtraOffsetsWithLeft:(CGFloat)left top:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom {
    self.setOffTop = top;
    self.setOffLeft = left;
    self.setOffRight = right;
    self.setOffBottom = bottom;
    
    
}

- (void)setValueArray:(NSArray *)valueArray {
    _valueArray = valueArray;
    self.circleArcView.valueDataArray = valueArray;
}

- (void)setColorArray:(NSArray *)colorArray {
    _colorArray = colorArray;
    self.circleArcView.colorArray = colorArray;
    self.bottomView.colorArray = colorArray;
}

- (void)setNameArray:(NSArray *)nameArray {
    _nameArray = nameArray;
}

/**
 *  设置圆弧之间的间距
 *
 *  @param sliceSpace 间距
 */
- (void)setSliceSpace:(CGFloat)sliceSpace {
    _sliceSpace = sliceSpace;
    self.circleArcView.sliceSpace = sliceSpace;
}

#pragma mark -底部显示设置

- (void)setLegendStyle:(ChartLegendStyle)legendStyle {
    _legendStyle = legendStyle;
    
    self.bottomView.legendStyle = legendStyle;
}

#pragma mark - BottomViewDelegate

- (void)bottomViewWidth:(CGFloat)width {
    
    switch (self.bottomViewStyle) {
        case PositionStyleLeft:
            self.bottomView.frame = CGRectMake(10, self.bounds.size.height - 30, width, 20);
            break;
        case PositionStyleCenter:
            self.bottomView.frame = CGRectMake((self.width - width) * 0.5, self.bounds.size.height - 30, width, 20);
            break;
        case PositionStyleRight:
            self.bottomView.frame = CGRectMake(self.width - width - 30, self.bounds.size.height - 30, width, 20);
            break;
        default:
            self.bottomView.frame = CGRectMake(10, self.bounds.size.height - 30, width, 20);
            break;
    }
    self.bottomBtn.frame = CGRectMake( CGRectGetMaxX(self.bottomView.frame) + 5, self.bounds.size.height - 30, CGRectGetHeight(self.bottomView.frame), CGRectGetHeight(self.bottomView.frame));
}

/**
 *  底部按钮单击方法
 */
- (void)bottomAction {
    if ([_delegate respondsToSelector:@selector(PieChartViewAction)]) {
        [_delegate PieChartViewAction];
    }
}

#pragma mark - 标题设置
/**
 *  设置标题名称
 *
 *  @param titleName 标题名称
 */
- (void)setTitleName:(NSString *)titleName {
    _titleName = titleName;
    self.titleNameLbl.text = titleName;
}

/**
 *  设置标题位置
 *
 *  @param style 位置
 */
- (void)setTitleStyle:(PositionStyle)titleStyle {
    switch (titleStyle) {
        case PositionStyleLeft:
            self.titleNameLbl.textAlignment = NSTextAlignmentLeft;
            break;
        case PositionStyleCenter:
            self.titleNameLbl.textAlignment = NSTextAlignmentCenter;
            break;
        case PositionStyleRight:
            self.titleNameLbl.textAlignment = NSTextAlignmentRight;
            break;
            
        default:
            self.titleNameLbl.textAlignment = NSTextAlignmentCenter;
            break;
    }
}

/**
 *  设置标题颜色
 *
 *  @param titleColor 颜色
 */
-(void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    self.titleNameLbl.textColor = titleColor;
}

/**
 *  设置标题字体的样式和大小
 *
 *  @param titleFont 字体的样式和大小
 */
- (void)setTitleFont:(UIFont *)titleFont {
    _titleFont = titleFont;
    self.titleNameLbl.font = titleFont;
}


- (MXCircleArcView *)circleArcView {
    if (!_circleArcView) {
        _circleArcView = [[MXCircleArcView alloc] init];
        _circleArcView.backgroundColor = [UIColor whiteColor];
    }
    return _circleArcView;
}

- (UILabel *)titleNameLbl {
    if (!_titleNameLbl) {
        _titleNameLbl = [[UILabel alloc] init];
        _titleNameLbl.textAlignment = NSTextAlignmentCenter;
        _titleNameLbl.font = [UIFont systemFontOfSize:15];
        _titleNameLbl.textColor = [UIColor blackColor];
    }
    return _titleNameLbl;
}

- (MXBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[MXBottomView alloc] init];
        _bottomView.backgroundColor = [UIColor clearColor];
        self.bottomView.frame = CGRectMake(10, self.height - 30, 20, 20);
        _bottomView.delegate = self;
    }
    return _bottomView;
}


- (UIButton *)bottomBtn {
    if (!_bottomBtn) {
        _bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_bottomBtn setBackgroundImage:[UIImage imageNamed:@"问号"] forState:UIControlStateNormal];
        [_bottomBtn setBackgroundImage:[UIImage imageNamed:@"问号"] forState:UIControlStateHighlighted];
        [_bottomBtn addTarget:self action:@selector(bottomAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bottomBtn;
}


@end
